package com.robusta.cityuate.services;


import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;

import com.robusta.cityuate.utils.ConnectionUtils;

public class MyReservationsService extends HTTPService {

	private static final String REQUEST_URL = "/reservations";
	private static final String OLD="/old";

	public static final String IS_COMING = "iscoming";
	
	private Context mContext=this;

	@Override
	protected void onResponse() {
		Intent getSpotUIupdate = new Intent(assignedFilter);
		switch (responseStatusCode) {
		case HttpStatus.SC_OK: {// successful login
			// remove connection from shared pref
			ConnectionUtils.setDisconnecting(mContext, true, assignedFilter,
					responseString);
			// update UI if active
			getSpotUIupdate.putExtra(RESUlT, true);
			sendOrderedBroadcast(getSpotUIupdate, null);
			break;
		}
		case HttpStatus.SC_NOT_FOUND: {
			JSONObject res;
			try {
				res = new JSONObject(responseString);
				onNoResponse(res.getString(MESSAGE));
			} catch (JSONException e) {
				onNoResponse(UNKNOWN_RESPONSE);
			}
		}
		default:
			onNoResponse(UNKNOWN_RESPONSE);
		}

	}

	@Override
	protected void onNoResponse(String msg) {
		// remove connection from shared pref keeping result and reason
		ConnectionUtils.setDisconnecting(mContext, false, assignedFilter, msg);
		// update UI if active
		Intent getSpotUIupdate = new Intent(assignedFilter);
		getSpotUIupdate.putExtra(RESUlT, false);
		sendOrderedBroadcast(getSpotUIupdate, null);
	}

	@Override
	protected HttpRequestBase initRequest() {
		boolean isComming=extras.getBoolean(IS_COMING);
		HttpGet req = new HttpGet(API_URL + REQUEST_URL+(isComming? "":OLD));
		return req;
	}

}
